<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="100px"
    >
      <el-form-item label="内码编号" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入内码编号"
          clearable
          class="!w-240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="内码名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入内码名称"
          clearable
          class="!w-240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="织板编码" prop="materialCode">
        <el-input
          v-model="queryParams.materialCode"
          placeholder="请输入织板编码"
          clearable
          class="!w-240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="批次号" prop="batchNumber">
        <el-input
          v-model="queryParams.batchNumber"
          placeholder="请输入批次号"
          clearable
          class="!w-240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单编号" prop="orderCode">
        <el-input
          v-model="queryParams.orderCode"
          placeholder="请输入订单编号"
          clearable
          class="!w-240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="批次校验序号" prop="batchCheckNumber">
        <el-input
          v-model="queryParams.batchCheckNumber"
          placeholder="请输入批次校验序号"
          clearable
          class="!w-240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单规格" prop="flex">
        <el-input
          v-model="queryParams.flex"
          placeholder="请输入订单规格"
          clearable
          class="!w-240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="长度" prop="len">
        <el-input-number
          v-model="queryParams.len"
          placeholder="请输入长度"
          clearable
          class="!w-240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExportTxt"
          :loading="exportLoadingTxt"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出文本
        </el-button>

        <!--          <el-button type="primary" @click="printHandle">打印</el-button>-->
      </el-form-item>
    </el-form>
  </ContentWrap>
  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" max-height="600">
      <el-table-column type="index" label="序号" align="center" width="55" />
      <el-table-column label="内码编号" align="center" prop="id" />
      <!-- <el-table-column label="内码名称" align="center" prop="name" /> -->
      <el-table-column label="订单编号" align="center" prop="orderCode" />
      <el-table-column label="织板编码" align="center" prop="materialCode" />
      <el-table-column label="批次号" align="center" prop="batchNumber" />
      <el-table-column label="批次校验序号" align="center" prop="batchCheckNumber" />
      <!-- <el-table-column label="备注" align="center" prop="remark" /> -->
      <el-table-column label="产品规格" align="center" prop="flexType" />
      <el-table-column label="长度" align="center" prop="len" />
      <el-table-column label="订单类型" align="center" prop="orderType" />
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
  <!-- 打印 -->
  <printForm ref="formRef" @success="getList" />
</template>

<script lang="ts" setup>
import download from '@/utils/download'
import * as internalCodeApi from '@/api/hcxly/internalCode/Index'
import printForm from './printForm.vue'

defineOptions({ name: 'internalCode' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const total = ref(0) // 列表的总页数
const list = ref([]) // 列表的数据
const queryParams = reactive({
  pageNo: 1,
  pageSize: 100,
  id: undefined,
  name: undefined,
  materialCode: undefined,
  batchNumber: undefined,
  orderCode: '',
  batchCheckNumber: undefined,
  flex: undefined,
  len: undefined,
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中
const exportLoadingTxt = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await internalCodeApi.getInternalCode(queryParams)
    // console.log(data)
    list.value = data.list
    total.value = data.total
    // const data2 = await internalCodeApi.getInternalCodeList(queryParams)
    // // console.log(data2)
    // list2.value = data2.list
  } finally {
    loading.value = false
  }
}

const list2 = ref([]) // 列表的数据
/** 查询列表不分页 */
const getListNoPage = async () => {
  const data = await internalCodeApi.getInternalCodeList(queryParams)
  // console.log(data)
  list2.value = data.list
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await internalCodeApi.exportInternalCode(queryParams)
    download.excel(data, '内码导出数据.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 导出文本按钮操作 */
const handleExportTxt = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoadingTxt.value = true
    const data = await internalCodeApi.exportInternalCodeTxt(queryParams)
    download.excel(data, '内码导出Txt数据.txt')
  } catch {
  } finally {
    exportLoadingTxt.value = false
  }
}

/** 添加/修改操作 */
const formRef = ref()
const printHandle = () => {
  // getListNoPage()
  formRef.value.open(list.value)
}

/** 初始化 **/
onMounted(() => {
  getList()
})


</script>
